import Web3, { Numbers } from "web3";

export function wei2Eth(wei: Numbers, decimalPlaces = 3, unit = 9) {
  if (!wei) return "0";
  const d = 10 ** decimalPlaces;
  return Math.floor(Number(Web3.utils.fromWei(wei, unit)) * d) / d + "";
}

export function eth2Wei(eth: Numbers, unit = 9) {
  if (!eth) return "0";
  return Web3.utils.toWei(eth, unit);
}

export const UINT256_MAX = "1";

export const formatNumber = (num: any): string => {
  return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
};

export const formatAddress = (address: string): string => {
  return address.slice(0, 6) + "..." + address.slice(-4);
};

export function getHexTimestamp() {
  // 获取当前时间戳（秒级）
  const timestamp = Date.now();
  // 将时间戳转换为十六进制
  const hexTimestamp = "0x" + timestamp.toString(16);
  return hexTimestamp;
}

/**
 * 复制文本
 * @param text
 */
export function copyToClipboard(text: string) {
  try {
    navigator.clipboard
      .writeText(text)
      .then(() => {
        console.log("复制成功:", text);
        alert("已复制到剪贴板！");
      })
      .catch((err) => {
        console.error("复制失败:", err);
        alert("复制失败，请手动复制");
      });
  } catch (e) {
    console.error("复制失败:", e);
  }
}

export function isEmptyAddress(address: string) {
  return !address || address === "0x0000000000000000000000000000000000000000";
}
